const Koa = require('koa');
const next = require('next');
const path = require('path');

const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const staticFiles = require('koa-static');
const handle = app.getRequestHandler();
const server = new Koa();

app
  .prepare()
  .then(() => {
    server.use(staticFiles(path.join(__dirname, 'static')));

    server.use(async (ctx, next) => {
      console.log(ctx, 'xxdddd');
      // await handle(ctx.req, ctx.res);

      ctx.set('Access-Control-Allow-Origin', '*');
      ctx.set('Access-Control-Allow-Headers', 'X-Requested-With');
      ctx.set('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS');
      // ctx.response = false;
      await next();
    });

    server.listen(8002, () => {
      console.log('koa server ....');
    });
  })
  .catch((e) => {
    console.log(e, 'error');
  });
